/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package im.filters;

import im.eventpackage.Predicate_old;
import im.eventpackage.EventUtils_old;
import java.util.StringTokenizer;
import java.util.Vector;
import im.subscriptionpackage.*;

/**
 *
 * @author Giovanni 
 */
public class ContentEventFilter_old {

    public ContentEventFilter_old() {
    }

    Predicate_old searchByName(Vector content, String name) {
        Predicate_old p = new Predicate_old(null, -1);

        int size = content.size();
        for (int i = 0; i < size; i++) {
            Predicate_old p1 = new Predicate_old(null, -1);
            p1 = (Predicate_old) content.get(i);
            if (p1.getName().equals(name)) {
                p = p1;
            }
        }

        return p;
    }
// modificare filterSubscription(evento,sub)
    boolean filterSubscription(EventUtils_old ev, ContentSubscription sub) {

        boolean ret = false;

        StringTokenizer st = new StringTokenizer(sub.getCriteria());
        String name = null;
        Object value = null;
        int bool = 0; //1:and 2:or

        Vector pairCont = new Vector();
        Vector boolCont = new Vector();
        Vector compCont = new Vector();

        //parsamo
        while (st.hasMoreTokens()) {

            String str = st.nextToken();

            if (str.equals("name") || str.equals("value")) {
                name = str;
            } else if (str.equals("<") || str.equals(">") || str.equals("==") || str.equals("<=") || str.equals(">=")) {
                compCont.addElement(str);
            } else if (str.equals("and")) {
                bool = 1;
                boolCont.add(bool);
            } else if (str.equals("or")) {
                bool = 2;
                boolCont.add(bool);
            } else {
                value = str;
            }

            if (name != null && value != null) {
                Predicate_old p = new Predicate_old(null, null);
                p.setName(name);
                p.setValue(value);
                //System.out.println("inseriamo: " + p.getName() + " " + p.getValue());
                pairCont.add(p);
                name = null;
                value = null;

            }

        }

        int size = pairCont.size();
        boolean m = false;
        boolean boolRes[];
        boolRes = new boolean[size];

        for (int i = 0; i < size; i++) {

            Predicate_old p1 = (Predicate_old) pairCont.get(i);
            Predicate_old p2 = searchByName(ev.getEvContainer(), p1.getName());

            if (compCont.get(i).equals("==")) {
                System.out.println("i: " + i + " | p1.name:" + p1.getName() + " | p2.name:" + p2.getName() + " | p1.value:" + p2.getValue() + " == " + " p2.value:" + p1.getValue());
                if (p1.getValue().equals(p2.getValue())) {
                    m = true;
                    System.out.println("true");
                } else {
                    m = false;
                    System.out.println("false");
                }
            }
            if (compCont.get(i).equals("<")) {
                System.out.println("i: " + i + " | p1.name:" + p1.getName() + " | p2.name:" + p2.getName() + " | p1.value:" + p2.getValue() + " < " + " p2.value:" + p1.getValue());
                if (Integer.parseInt(p2.getValue().toString()) < Integer.parseInt(p1.getValue().toString())) {
                    m = true;
                    System.out.println("true");
                } else {
                    m = false;
                    System.out.println("false");
                }
            }
            if (compCont.get(i).equals(">")) {
                System.out.println("i: " + i + " | p1.name:" + p1.getName() + " | p2.name:" + p2.getName() + " | p1.value:" + p2.getValue() + " > " + " p2.value:" + p1.getValue());
                if (Integer.parseInt(p2.getValue().toString()) > Integer.parseInt(p1.getValue().toString())) {
                    m = true;
                    System.out.println("true");
                } else {
                    m = false;
                    System.out.println("false");
                }
            }
            if (compCont.get(i).equals("<=")) {
                System.out.println("i: " + i + " | p1.name:" + p1.getName() + " | p2.name:" + p2.getName() + " | p1.value:" + p2.getValue() + " <= " + " p2.value:" + p1.getValue());
                if (Integer.parseInt(p2.getValue().toString()) <= Integer.parseInt(p1.getValue().toString())) {
                    m = true;
                    System.out.println("true");
                } else {
                    m = false;
                    System.out.println("false");
                }
            }
            if (compCont.get(i).equals(">=")) {
                System.out.println("i: " + i + " | p1.name:" + p1.getName() + " | p2.name:" + p2.getName() + " | p1.value:" + p2.getValue() + " >= " + " p2.value:" + p1.getValue());
                if (Integer.parseInt(p2.getValue().toString()) >= Integer.parseInt(p1.getValue().toString())) {
                    m = true;
                    System.out.println("true");
                } else {
                    m = false;
                    System.out.println("false");
                }
            }
            boolRes[i] = m;
        }

        //operazione booleana predicati

        if (pairCont.size() == 1) {

            ret = m;

        } else {
            boolean f = true;
            for (int j = 0; j < size; j++) {

                if (j > 0) {

                    int v = Integer.parseInt(boolCont.get(j - 1).toString());
                    if (v == 1) {
                        f = boolRes[j] && f;
                    }
                    if (v == 2) {
                        f = boolRes[j] || f;
                    }
                } else {
                    f = boolRes[j];
                }
            }
            ret = f;

        }
        return ret;

    }
}